package com.cls.system.controller;

import cn.hutool.core.util.RandomUtil;
import com.cls.business.service.ISysSmsService;
import com.cls.common.annotation.Limit;
import com.cls.common.entity.ResponseVO;
import com.cls.common.enums.EmailTextEnum;
import com.cls.common.exception.MyException;
import com.cls.common.service.RedisService;
import com.cls.common.service.ValidateCodeService;
import com.cls.common.utils.EmailSenderUtil;
import com.cls.system.entity.User;
import com.cls.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;

/**
 * @auth 魏起成
 */
@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("register")
public class RegisterController {

    private final ValidateCodeService validateCodeService;

    private final RedisService redisService;

    private final IUserService userService;
    private final ISysSmsService smsService;
    private final EmailSenderUtil emailSenderUtil;

    //短信验证码
//    private final String smsCode = "123456";

    //短信验证码过期时间
    private final Long SMS_CODE_EXPIRE_SECONDS = 180L;

    /**
     * 获取短信验证码
     */
    @GetMapping("getEmailCode/{email}")
    @Limit(key = "get_register_sys_code", period = 60, count = 10, name = "获取短信验证码", prefix = "limit")
    @ResponseBody
    public ResponseVO getSmsCode(@NotBlank(message = "{required}") @PathVariable String email, @NotBlank(message = "{required}") String verifyCode, HttpServletRequest request) {
        //验证验证码输入是否正确
        HttpSession session = request.getSession();
        validateCodeService.check(session.getId(), verifyCode);
        //判断是否过期
        Long expire = redisService.getExpire(email);
        if (expire > 0) {
            return ResponseVO.success("验证码已发送，请在邮箱查收。");
        }
        String smsCode = RandomUtil.randomNumbers(6);
        HashMap<String, String> senCode = new HashMap<>();
        senCode.put("code", smsCode);
        emailSenderUtil.sendEmail(EmailTextEnum.REGISTER_USER.getText(), String.format(EmailTextEnum.REGISTER_USER_TEXT.getText(), smsCode), email);
//        需求调整需要改为邮箱号注册
//        smsService.sendSMS(DictEnum.REGISTER_USER, email, senCode);
        redisService.set(email, smsCode, SMS_CODE_EXPIRE_SECONDS);
        return ResponseVO.success();
    }

    @PostMapping("create")
    @ResponseBody
    public ResponseVO regist(
            @NotBlank(message = "{required}") String username,
            @NotBlank(message = "{required}") String password,
            @NotBlank(message = "{required}") String email,
            @NotBlank(message = "{required}") String smsCode) throws MyException {
        String smsCodeRedis = (String) redisService.get(email);
        if (smsCodeRedis == null) {
            return ResponseVO.failure("邮箱验证码已过期！");
        }
        if (!smsCodeRedis.equals(smsCode)) {
            return ResponseVO.failure("邮箱验证码不正确！");
        }
        User user = userService.findByName(username);
        if (user != null) {
            throw new MyException("该用户名已存在");
        }
        this.userService.registFront(username, password, email);
        return ResponseVO.success();
    }


}
